package hk.linkcredit.walletservice.wallet.service.strategy;

import hk.linkcredit.walletservice.constant.Status;
import hk.linkcredit.walletservice.constant.WalletConstants;
import hk.linkcredit.walletservice.exception.BizException;
import hk.linkcredit.walletservice.utils.StringUtils;
import hk.linkcredit.walletservice.wallet.domain.entity.WalletOrder;
import hk.linkcredit.walletservice.wallet.domain.vo.WalletOrderReq;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @Author: Jamnsin
 * @Date: 2022/6/12 16:26
 */
@Slf4j
@Component(WalletConstants.WALLET_ORDER_STATUS_1)
public class OrderApprovedStrategy extends OrderStatusService implements OrderStatusStrategy {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void dealWalletOrder(WalletOrderReq walletOrderReq) {
        log.info("开始处理钱包订单状态 >>> orderStatus = {}, walletOrderReq = {}", WalletConstants.WALLET_ORDER_STATUS_APPROVED, walletOrderReq);

        WalletOrder walletOrder = this.getWalletOrder(walletOrderReq.getCustNo(), walletOrderReq.getOrderSerialNo());

        log.info("当前订单状态：currentStatus = {}，targetStatus = {}", walletOrder.getOrderStatus(), WalletConstants.WALLET_ORDER_STATUS_APPROVED);
        if (!StringUtils.equals(walletOrder.getOrderStatus(), WalletConstants.WALLET_ORDER_STATUS_UNDER_REVIEW)) {
            throw new BizException(Status.WALLET_ORDER_STATUS_EXPIRED);
        }

        WalletOrder walletOrderStandby = WalletOrder.builder().id(walletOrder.getId()).orderStatus(WalletConstants.WALLET_ORDER_STATUS_APPROVED).updateBy(walletOrderReq.getReviewer()).build();
        this.updateWalletOrder(walletOrderStandby);
    }
}
